/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ControlVentas.java
 *
 * Created on 08-sep-2012, 22:14:45
 */
package formularios;

import clases.ConversorRSaDefaultTableModel;
import clases.ProcesarTxt;
import clases.abmBovino;
import clases.conexion;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Elio
 */
public class ControlVentas extends javax.swing.JDialog {
    String consultainicial="";
    String identificacion="";
    /** Creates new form ControlVentas */
    public ControlVentas(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setTitle("Control de Ventas de Animales");
        consultainicial="SELECT Fecha, count(identificacion) as Cantidad FROM controles c where idControl=5 group by fecha";
        CrearTabla(consultainicial,tablaStock,modelo);
    }
  public void Conectar(){
        try{
            mdbc = new conexion();
            mdbc.init();
            Connection conn= mdbc.getMyConnection();
            stmt = conn.createStatement();
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Problemas al concetarse a la Base de Datos","Error",JOptionPane.ERROR_MESSAGE);
        }
    }
    
     private void CrearTabla(String sql, JTable tabla, DefaultTableModel mode){
      
          try {
            Conectar();
            ResultSet rs = stmt.executeQuery(sql);

      
            tabla.setModel(mode);
            tabla.setAutoResizeMode(tabla.AUTO_RESIZE_ALL_COLUMNS);
              
                    
            ConversorRSaDefaultTableModel.completar(rs, mode);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error en la consulta de control preñez" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        
    }
    private conexion mdbc;
    private Statement stmt;
    private DefaultTableModel modelo = new DefaultTableModel();
    private DefaultTableModel modelo2 = new DefaultTableModel();
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnNuevoControlVentas = new javax.swing.JButton();
        panelConsultas = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaStock = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Control Ventas");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Control de Venta de Animales");

        jPanel1.setLayout(new java.awt.GridLayout(1, 2));

        btnNuevoControlVentas.setText("Nuevo Control");
        btnNuevoControlVentas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoControlVentasActionPerformed(evt);
            }
        });
        jPanel1.add(btnNuevoControlVentas);

        tablaStock.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tablaStock.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaStockMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablaStock);

        javax.swing.GroupLayout panelConsultasLayout = new javax.swing.GroupLayout(panelConsultas);
        panelConsultas.setLayout(panelConsultasLayout);
        panelConsultasLayout.setHorizontalGroup(
            panelConsultasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelConsultasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );
        panelConsultasLayout.setVerticalGroup(
            panelConsultasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelConsultasLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(panelConsultas, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelConsultas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-343)/2, (screenSize.height-344)/2, 343, 344);
    }// </editor-fold>//GEN-END:initComponents

private void btnNuevoControlVentasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoControlVentasActionPerformed
    new ProcesarTxt().LeerControlVentas(5);
    CrearTabla(consultainicial, tablaStock,modelo);
    
}//GEN-LAST:event_btnNuevoControlVentasActionPerformed

private void tablaStockMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaStockMouseClicked
         int fila = tablaStock.getSelectedRow();
       
       if ((fila > -1)){
            String fecha= String.valueOf(tablaStock.getValueAt(fila,0)); 
            String consulta= "SELECT c.identificacion,b.NumSenasa as 'N° Senasa',"
                    + " b.sexo, p.descripcion as 'Pelaje', "
                    + "tb.descripcion as 'Categoria', "
                    + "b.ColorCaravana as 'Color caravana'  "
                    + "FROM controles c, bovino b, pelaje p, tipobovino tb where "
                    + "idControl=5 and fecha='"+fecha+"' and "
                    + " c.identificacion=b.identificacion and"
                    + " b.idtipobovino = tb.idtipobovino and "
                    + "b.idpelaje = p.idpelaje";
//            CrearTabla(consulta,existentes,modelo2);
            System.out.println(fecha);
       }
           
            
   
}//GEN-LAST:event_tablaStockMouseClicked

 public String convertirFecha(String FechaA){
      String anio,mes,dia;
     //30112011
      anio=FechaA.substring(6,10);
      mes=FechaA.substring(3,5);
      dia=FechaA.substring(0,2);
      String Fecha=anio+"-"+mes+"-"+dia;
      return Fecha;

    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ControlVentas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ControlVentas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ControlVentas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ControlVentas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ControlVentas dialog = new ControlVentas(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    private boolean ninja = false;
    private String fecha;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNuevoControlVentas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelConsultas;
    private javax.swing.JTable tablaStock;
    // End of variables declaration//GEN-END:variables
}
